package com.atguigu.gmall.product.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.api.BaseTrademarkService;
import com.atguigu.gmall.product.service.api.ManagerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author: 封捷
 * @create-date: 2022/8/23 15:27
 */
@Api(tags = "商品数据汇总接口")
@RestController

// /api/product地址表示当前 Controller 暴露接口供 OpenFeign 进行远程调用
@RequestMapping("/api/product")
public class ProductAPIController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private BaseTrademarkService trademarkService;

    // inner 仅仅表示这是一个内部地址
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {

        SkuInfo skuInfo = managerService.getSkuInfoById(skuId);

        return Result.ok(skuInfo);
    }

    @GetMapping("/inner/getCategoryView/{category3Id}")
    public Result<BaseCategoryView> getCategoryView(
            @PathVariable("category3Id") Long category3Id) {

        BaseCategoryView baseCategoryView = managerService.getCategoryViewBy3Id(category3Id);

        return Result.ok(baseCategoryView);
    }

    @GetMapping("/inner/getSkuPrice/{skuId}")
    public Result<BigDecimal> getSkuPrice(@PathVariable("skuId") Long skuId) {

        BigDecimal price = managerService.getSkuPriceById(skuId);

        return Result.ok(price);
    }

    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrListCheckBySku(
            @PathVariable("skuId") Long skuId,
            @PathVariable("spuId") Long spuId) {
        return Result.ok(managerService.getSpuSaleAttrAndValueWithCheck(skuId, spuId));
    }

    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public Result<Map<String, Object>> getSkuValueIdsMap(
            @PathVariable("spuId") Long spuId) {

        Map<String, Object> map = managerService.getSaleAttrValuesBySpu(spuId);

        return Result.ok(map);
    }

    @GetMapping("/inner/get/category/for/portal")
    public Result<List<JSONObject>> getCategoryForPortal() {
        return Result.ok(managerService.getBaseCategoryList());
    }

    @GetMapping("/inner/get/attr/{skuId}")
    public Result<List<BaseAttrInfo>> getBaseAttrInfoBySkuId(
            @PathVariable("skuId") Long skuId) {

        List<BaseAttrInfo> baseAttrInfoList = managerService.getBaseAttrInfoListBySkuId(skuId);

        return Result.ok(baseAttrInfoList);
    }

}
